/*
 * Copyright (c) 2021，上海福宇龙汽车科技有限公司
 * All rights reserved.
 * 文件名称: DRV.h
 * 摘要:
 * 当前版本: V1.0.0 WCD
 * Date: 2021-11-01
 * 历史版本: Vx.x.x，编写者/修改者，修改时间，修改内容（重大发布时，总结重点变更内容）
 */
   
#ifndef DRV_H
#define DRV_H
/*PRQA S 1534 ++*/
/*使能取消assert宏功能*/
/* #define NDEBUG */

#include <assert.h>

#include <string.h>
#include "io.h"
#include "drv_typedef.h"

//#define PWM_TEST
/*开启assert功能*/
#define USE_DRV_ASSERT

/*使能错误调试功能*/
#define CODE_ERROR_DEBUG

#ifdef USE_DRV_ASSERT
#define DRV_ASSERT(condition) assert(condition)
#else
#define DRV_ASSERT(error_message) (void(0))
#endif

#define Reserved_Value (0U)


/* 空函数 */
void _CPU_NOP(void);

#define NOP                   __NOP()

/* For debugging, do not attempt to modify vars other than key  */
#define ON              (1U)
#define OFF             (0U)

/* 全局中断控制 禁止中断 */
#define DRV_IT_DISABLE __DI()

/* 全局中断控制 打开中断 */
#define DRV_IT_ENABLE  __EI()

#define Reserved_Value (0U)
#define REG64(addr) ((__IO uint64_t*)(addr))
#define REG32(addr) ((__IO uint32_t*)(addr))
#define REG16(addr) ((__IO uint16_t*)(addr))
#define REG8(addr) ((__IO uint8_t*)(addr))

#define RMWREG64(addr, startbit, width, val) \
    *REG64(addr) = (*REG64(addr) & ~(((1 << (width)) - 1) << (startbit))) | ((val) << (startbit))
#define RMWREG32(addr, startbit, width, val) \
    *REG32(addr) = (*REG32(addr) & ~(((1 << (width)) - 1) << (startbit))) | ((val) << (startbit))
#define RMWREG16(addr, startbit, width, val) \
    *REG16(addr) = (*REG16(addr) & ~(((1 << (width)) - 1) << (startbit))) | ((val) << (startbit))
#define RMWREG8(addr, startbit, width, val) \
    *REG8(addr) = (*REG8(addr) & ~(((1 << (width)) - 1) << (startbit))) | ((val) << (startbit))

#define RMW(reg, startbit, width, val) \
    (reg) = ((reg) & ~(((1 << (width)) - 1) << (startbit))) | ((val) << (startbit))

#define writel(v, a) (*REG32(a) = (v))
#define readl(a) (*REG32(a))

/*  param: REG-->the pointer to register 指向寄存器的指针*/

#define SET_BIT(REG, BIT) ((*REG) |= (BIT))

#define CLEAR_BIT(REG, BIT) ((*REG) &= ~(BIT))

#define SET_BIT_POS(REG, BIT) ((*REG) |= (1U<<BIT))
#define CLEAR_BIT_POS(REG, BIT) ((*REG) &= ~(1U<<BIT))

#define Reset_Bit(REG, BIT) ((*REG) &= ~(BIT))
#define READ_BIT(REG, BIT) ((*REG) & (BIT))

#define CLEAR_REG(REG) ((*REG) = (0x0U))

#define WRITE_REG(REG, VAL) ((*REG) = (VAL))

#define READ_REG(REG) ((*REG))

#define MODIFY_REG(REG, CLEARMASK, SETMASK) \
    WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))

#define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))

#define DRV_WRITE_Reg(reg, value) \
    do \
    { \
    *(reg) = (value); \
    } while (0U)

#define DRV_READ_Reg(reg) (*(reg))

#ifdef CODE_ERROR_DEBUG
#define Code_Error do \
    { \
    while(1U){NOP;};\
}while(0U)
#else
#define Code_Error ()
#endif

#ifdef CODE_ERROR_DEBUG
#define Pointer_Null_Error do \
    { \
    while(1U){NOP;};\
}while(0U)
#else
#define Pointer_Null_Error()
#endif

#endif
